<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;
use Mail;

class ZhuceController extends Controller
{
   public function getIndex(){
    //加载注册页面
    return view('home.zhuce.index');
   }

   public function postInsert(Request $request){
        // dd($request -> all());
        // 验证非法字段 and ajax验证用户名是否存在提示错误信息

        // 验证两次密码
        if($request -> input('password') != $request -> input('repassword')){
            return back() -> withInput() -> with('error','两次密码不一致');
        }
        //验证验证码
        if($request -> input('code') != session('code')){
            return back() -> withInput() -> with('error','验证码都错了，你寻思啥呢！！');
        }
        //处理数据 邮箱 密码 事件 token
        $data = $request -> only(['email','password']);
        $data['password'] = Hash::make($data['password']);
        $data['ctime'] = time();
        $data['token'] = str_random(50);
        //发送邮件
        $id =  DB::table('user') -> insertGetId($data);
        if($id > 0){
            //发送邮件 发送人 $data['email']  id   token $data['token']
            self::mailto($data['email'],$id,$data['token']);
        }
        
    }
        static public function mailto($email,$id,$token){
            // echo 1;die;
            //用哪个模板发送 模板名称 694305409
            Mail::send('email.index', ['id' => $id,'token'=>$token], function ($m) use ($email) {
                $m -> to($email)->subject('这是一封激活邮件');
            });
   }
        public function getActivate(){
            //验证token
            // dd($_GET);
            $res = DB::table('user') -> where('id',$_GET['id']) -> select('token') -> first();
                // var_dump($res);
            if($res['token'] == $_GET['token']){
                $s = DB::table('user')-> where('id',$_GET['id'])-> update(['status'=>1,'token'=>str_random(50)]);    
            }
            
            if ($s > 0) {
                echo '激活成功';
            }else{
                echo '激活失败';
            }
        }

 
        static public function xml_to_array($xml){
            $arr = [];
            $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
            if(preg_match_all($reg, $xml, $matches)){
                $count = count($matches[0]);
                for($i = 0; $i < $count; $i++){
                $subxml= $matches[2][$i];
                $key = $matches[1][$i];
                    if(preg_match( $reg, $subxml )){
                        $arr[$key] = self::xml_to_array( $subxml );
                    }else{
                        $arr[$key] = $subxml;
                    }
            }
        }
    return $arr;
}
//请求数据到短信接口，检查环境是否 开启 curl init。
static public function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
}

        public function postPhone(){
            //短信接口地址
            $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
            //获取手机号
            $mobile = $_POST['mobile'];
            session(['phone'=> rand(1000,9999)]);
            //生成的随机数
            $mobile_code = session('phone');
            if(empty($mobile)){
                exit('手机号码不能为空');
            }

            $post_data = "account=C10621608&password=5b49ca8d8230dd4734948f92d7ff38e4&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$mobile_code."。请不要把验证码泄露给其他人。");
            //用户名请登录用户中心->验证码、通知短信->帐户及签名设置->APIID
            //查看密码请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
            $gets =  self::xml_to_array(self::Post($post_data, $target));
            if($gets['SubmitResult']['code']==2){
                $_SESSION['mobile'] = $mobile;
                // $_SESSION['mobile_code'] = $mobile_code;
            }
            echo $gets['SubmitResult']['msg'];
        }

        public function postInsert2(Request $request){
            
        }
}
